<template>
  <el-dialog :visible.sync="visible" :title="dataForm.classifyId?'编辑分类':'添加分类'" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="right" label-width="80px" style="width: 500px; margin-left:50px;">
        <el-form-item label="分类名称" prop="classfyName">
          <el-input v-model="dataForm.classfyName" placeholder="请选择分类名称"/>
        </el-form-item>
        <el-form-item label="关联组合" prop="compositionName">
          <el-input v-model="dataForm.compositionName" placeholder="请选择关联组合" readonly :disabled="localSet">
            <el-button slot="append" icon="el-icon-search" @click="selectHandle"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="投资规模" prop="initMoney">
          <el-input-number v-model="dataForm.initMoney" placeholder="请输入投资规模" :controls="false" :precision="2" style="width: 160px;"></el-input-number> 万元
        </el-form-item>
        <el-form-item label="对标指数" prop="indexType">
           <!-- :disabled="localSet&&dataForm.classifyId!=''" -->
          <indexSelectTree :multiple="false" :width="420" v-model="dataForm.indexType"></indexSelectTree>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input type="textarea" :rows="3" placeholder="最多120个字符" v-model="dataForm.mark" maxlength="120" size="mini" show-word-limit style="width: 500px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="addSubmit">提交</el-button>
      </div>

      <combManage v-if="combManageView" ref="combManage" :userId="userId" @selectcombManage="selctCompositionHandle"></combManage>
  </el-dialog>
</template>

<script>
import { addOrUpdateStrategyClassify } from '@/api/groupManage'
import indexSelectTree from '@/components/investComn/indexSelectTree.vue'
import combManage from './combManage.vue'
// import { deepClone } from '@/utils'

export default {
  props: {
    userId: String | Number,
    localSet: {
      type: Boolean,
      default: false
    }
  },
  components: { indexSelectTree, combManage },
  data() {
    return {
      visible: false,
      combManageView: false,
      dataForm: {
        compositionId: '',
        compositionName: '',
        classifyId: '',
        classfyName: '',
        indexType: '',
        mark: ''
      },
      rules: {
        classfyName: [{ required: true, message: '分类名称必填', trigger: 'change' }],
        compositionName: [{ required: true, message: '关联组合必填', trigger: 'change' }],
        initMoney: [{ required: true, message: '投资规模必填', trigger: 'blur' }],
        indexType: [{ required: true, message: '对标指数必填', trigger: 'change' }],
      }
    }
  },
  methods: {
    init(row) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm = {
          compositionId: '',
          compositionName: '',
          classifyId: '',
          classfyName: '',
          indexType: '',
          mark: ''
        }
        console.log(row)
        if(row&&row.classifyId) {
          this.dataForm = Object.assign({}, row)
        }
        if(row&&row.compositionId) {
          this.dataForm.compositionId = row.compositionId
          this.dataForm.compositionName = row.compositionName
        }
      })
    },
    selectHandle() {
      this.combManageView = true
      this.$nextTick(() => {
        this.$refs.combManage.init()
      })
    },
    selctCompositionHandle(row) {
      this.$set(this.dataForm, 'compositionName', row.compositionName)
      this.dataForm.compositionId = row.compositionId
    },
    addSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if(valid) {
          addOrUpdateStrategyClassify({
            ...this.dataForm,
            userId: Number(this.userId)
          }).then(res => {
            if(res.code == 0) {
              this.$notify({
                message: res.msg,
                type: 'success',
                duration: 2000
              })
              this.visible = false
              // if(this.localSet) {
              //   const res = deepClone(this.dataForm)
              //   this.$emit('closeOpen', res)
              // }else {
              //   this.$emit('closeOpen')
              // }
              this.$emit('closeOpen')
            }
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>

</style>


